package net.technics;

import java.util.ArrayList;
import java.util.List;

import net.models.Application;
import net.models.Etat;
import net.models.Fichefrais;
import net.models.Users;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class FraisForfaitStatut {
	private DataBindingContext	m_bindingContext;

	protected Shell				shellFraisForfaitStatut;
	private Text				txtModifierLesStatuts;
	private List<Fichefrais>	fichefraises	= new ArrayList<Fichefrais>();
	private List<Etat>			etats			= new ArrayList<Etat>();
	private List<Users>			users			= new ArrayList<Users>();
	private List<Fichefrais>	fichefrais		= new ArrayList<>();
	private ComboViewer			comboViewerUtilisateur;
	private ComboViewer			comboViewerStatut;
	private Table				table;
	private TableViewer			tableViewer;
	private List<Fichefrais>	ffs;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					FraisForfaitStatut window = new FraisForfaitStatut();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		fichefraises = Application.getFichefraises();
		etats = Application.getEtats();
		users = Application.getUsers();
		Display display = Display.getDefault();
		createContents();
		shellFraisForfaitStatut.open();
		shellFraisForfaitStatut.layout();
		while (!shellFraisForfaitStatut.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shellFraisForfaitStatut = new Shell();
		shellFraisForfaitStatut.setSize(997, 520);
		shellFraisForfaitStatut.setText("SWT Application");
		shellFraisForfaitStatut.setLayout(null);

		Composite composite_1 = new Composite(shellFraisForfaitStatut, SWT.NONE);
		composite_1.setBounds(9, 111, 725, 309);

		CLabel lblEtat = new CLabel(composite_1, SWT.NONE);
		lblEtat.setBounds(42, 10, 32, 26);
		lblEtat.setText("Etat");

		CLabel lblVisiteurs = new CLabel(composite_1, SWT.NONE);
		lblVisiteurs.setBounds(127, 10, 67, 26);
		lblVisiteurs.setText("Visiteurs");

		CLabel lblMois = new CLabel(composite_1, SWT.NONE);
		lblMois.setText("Mois");
		lblMois.setBounds(241, 10, 47, 26);

		CLabel lblLignefraishorsforfaits = new CLabel(composite_1, SWT.NONE);
		lblLignefraishorsforfaits.setBounds(296, 10, 119, 26);
		lblLignefraishorsforfaits.setText("Frais hors forfaits");

		CLabel lblFraisForfait = new CLabel(composite_1, SWT.NONE);
		lblFraisForfait.setBounds(421, 10, 81, 26);
		lblFraisForfait.setText("Frais forfait");

		CLabel lblMontantValid = new CLabel(composite_1, SWT.NONE);
		lblMontantValid.setBounds(511, 10, 106, 26);
		lblMontantValid.setText("Montant valid\u00E9");

		tableViewer = new TableViewer(composite_1, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setBounds(10, 42, 709, 267);

		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnEtat = tableViewerColumn.getColumn();
		tblclmnEtat.setWidth(100);

		tblclmnEtat.setText("Etat");

		TableViewerColumn tableViewerColumn_6 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnVisiteurs = tableViewerColumn_6.getColumn();
		tblclmnVisiteurs.setWidth(100);
		tblclmnVisiteurs.setText("Visiteurs");

		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnMois = tableViewerColumn_5.getColumn();
		tblclmnMois.setWidth(100);
		tblclmnMois.setText("Mois");

		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnFraisHorsForfaits = tableViewerColumn_4.getColumn();
		tblclmnFraisHorsForfaits.setWidth(100);
		tblclmnFraisHorsForfaits.setText("Frais Hors forfaits");

		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnFraisForfait = tableViewerColumn_3.getColumn();
		tblclmnFraisForfait.setWidth(100);
		tblclmnFraisForfait.setText("Frais forfait");

		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnMontantValid = tableViewerColumn_2.getColumn();
		tblclmnMontantValid.setWidth(100);
		tblclmnMontantValid.setText("Montant Validé");

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnCheck = tableViewerColumn_1.getColumn();
		tblclmnCheck.setWidth(100);
		tblclmnCheck.setText("checkBox");

		Composite composite = new Composite(shellFraisForfaitStatut, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
		composite.setBounds(0, 0, 984, 48);

		txtModifierLesStatuts = new Text(composite, SWT.NONE);
		txtModifierLesStatuts.setText("Fiche de frais des utilisateurs");
		txtModifierLesStatuts.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txtModifierLesStatuts.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		txtModifierLesStatuts.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
		txtModifierLesStatuts.setBounds(10, 10, 331, 21);

		Composite composite_2 = new Composite(shellFraisForfaitStatut, SWT.NONE);
		composite_2.setBounds(525, 421, 209, 52);

		Button button = new Button(composite_2, SWT.NONE);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shellFraisForfaitStatut.close();
			}
		});
		button.setText("Annuler");
		button.setBounds(106, 10, 90, 30);

		Button button_1 = new Button(composite_2, SWT.NONE);
		button_1.setText("Valider");
		button_1.setBounds(10, 10, 90, 30);

		Group grpRecherche = new Group(shellFraisForfaitStatut, SWT.NONE);
		grpRecherche.setText("Recherche");
		grpRecherche.setBounds(10, 50, 530, 55);

		CLabel lblStatut = new CLabel(grpRecherche, SWT.NONE);
		lblStatut.setBounds(10, 25, 61, 21);
		lblStatut.setText("Statut");

		comboViewerStatut = new ComboViewer(grpRecherche, SWT.NONE);
		Combo comboStatut = comboViewerStatut.getCombo();
		comboStatut.setBounds(72, 25, 160, 28);

		CLabel lblTrierPar = new CLabel(grpRecherche, SWT.NONE);
		lblTrierPar.setBounds(270, 25, 76, 21);
		lblTrierPar.setText("Utilisateur");

		comboViewerUtilisateur = new ComboViewer(grpRecherche, SWT.NONE);
		Combo comboUtilisateur = comboViewerUtilisateur.getCombo();
		comboUtilisateur.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

			}
		});
		comboUtilisateur.setBounds(352, 25, 165, 28);
		comboStatut.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ffs = fichefraises;
				IStructuredSelection sel1 = (IStructuredSelection) (comboViewerStatut.getSelection());
				Etat etat = (Etat) (sel1.iterator().next());
				IStructuredSelection sel2 = (IStructuredSelection) (comboViewerUtilisateur.getSelection());
				Users user = (Users) (sel2.iterator().next());
				for (Fichefrais ff : ffs) {
					if (!ff.getEtat().equals(etat)) {
						ffs.remove(ff);
					}
				}
			}
		});

		Shell shell = new Shell(SWT.BORDER | SWT.MAX);
		m_bindingContext = initDataBindings();

	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
		IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider_1.getKnownElements(), Users.class, new String[] { "name", "id", "prenom" });
		comboViewerUtilisateur.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		comboViewerUtilisateur.setContentProvider(listContentProvider_1);
		//
		IObservableList selfList_1 = Properties.selfList(Users.class).observe(users);
		comboViewerUtilisateur.setInput(selfList_1);
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap[] observeMap = BeansObservables.observeMaps(listContentProvider.getKnownElements(), Etat.class, new String[] { "libelle", "id" });
		comboViewerStatut.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		comboViewerStatut.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Etat.class).observe(etats);
		comboViewerStatut.setInput(selfList);
		//
		ObservableListContentProvider listContentProvider_2 = new ObservableListContentProvider();
		IObservableMap[] observeMaps_1 = BeansObservables.observeMaps(listContentProvider_2.getKnownElements(), Fichefrais.class, new String[] { "etat.libelle", "lignefraisforfaits", "lignefraishorsforfaits", "mois", "montantValide", "users.name" });
		tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMaps_1));
		tableViewer.setContentProvider(listContentProvider_2);
		//
		IObservableList selfList_2 = Properties.selfList(Fichefrais.class).observe(ffs);
		tableViewer.setInput(selfList_2);
		//
		return bindingContext;
	}
}
